package com.test.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.annotation.MultipartConfig;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Controller
@MultipartConfig
public class PublicFileRequest {

    //    文件上传
    @PostMapping("imgUpload/thumb")
    @ResponseBody
    public Map<String, String> postThumb(MultipartFile file) throws IOException {
        Map<String,String> map = new HashMap<>();
        String s = file.getOriginalFilename().toString();

        if (file.getSize()>0){
            String filename = UUID.randomUUID().toString().replaceAll("-", "");
            String substring = file.getOriginalFilename().substring(s.lastIndexOf("."));
            File files = new File("E:\\Users\\Administrator\\IdeaProjects\\mavens\\oa2\\src\\main\\webapp\\static\\sources\\"+filename+substring);
            file.transferTo(files);
            map.put("url","/sources/"+filename+substring);
            map.put("code","200");
        }
        return map;
    }

}
